﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.Mobile.ContainerModel;

namespace QuickStart
{
	internal class TaskService : ITaskService
	{
		Container container;

		public TaskService(Container container)
		{
			this.container = container;
			this.Tasks = new Dictionary<string, Action<Container>>();
		}

		public Dictionary<string, Action<Container>> Tasks { get; private set; }

		public IEnumerator<string> GetEnumerator()
		{
			return Tasks.Keys.GetEnumerator();
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		public void Register<TView>(string taskName) where TView : IView
		{
			// Store as a task an action that resolves the given view 
			// and invokes ShowDialog on it.
			Tasks[taskName] = c => c.Resolve<TView>().ShowDialog();
		}

		public void Execute(string taskName)
		{
			if (!Tasks.ContainsKey(taskName))
				return;

			using (var child = container.CreateChildContainer())
			{
				Tasks[taskName].Invoke(child);
			}
		}
	}
}
